class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        # 法一
        # bin_str = bin(n)[2:]
        # for i in range(len(bin_str)-1):
        #     if bin_str[i] == bin_str[i+1]:
        #         return False
        # return True
        # 法二
        tmp = n ^ (n >> 1)
        return (tmp & (tmp+1)) == 0
if __name__ == '__main__':
    n = 5
    print(Solution().hasAlternatingBits(n))